<?php
class DatetimeBase extends DateTime
{
    public static function Now()
    {
		return date('Y-m-d H:i:s');
    }

    public static function Today()
    {
		return date('Y-m-d');
    }

    public static function TimeId()
    {
		return date('YmdHis');
    }

    public static function AddDays($days, $basetime_unix = null)
    {
        if (is_null($basetime_unix)) $basetime_unix = time();
        return date('Y-m-d', $basetime_unix + $days*24*60*60);
    }

    public static function GetRelativeTime($date)
    {
        $diff = time() - strtotime($date);
        if ($diff<60)
            return $diff . ' second' . String::Plural($diff) . ' ago';
        $diff = round($diff/60);
        if ($diff<60)
            return $diff . ' minute' . String::Plural($diff) . ' ago';
        $diff = round($diff/60);
        if ($diff<24)
            return $diff . ' hour' . String::Plural($diff) . ' ago';
        $diff = round($diff/24);
        if ($diff<7)
            return $diff . ' day' . String::Plural($diff) . ' ago';
        $diff = round($diff/7);
        if ($diff<4)
            return $diff . ' week' . String::Plural($diff) . ' ago';
        return date('F j, Y', strtotime($date));
    }

    public static function GetCron($minute, $hour, $dayOfMonth, $month, $dayOfWeek)
    {
        return "$minute $hour $dayOfMonth $month $dayOfWeek";
    }

    public static function GetAge($date)
    {
        $dob = date("Y-m-d",strtotime($date));
        $ageparts = explode("-",$dob);
        $age = date("Y-m-d")-$dob;
        return (date("nd") < $ageparts[1].str_pad($ageparts[2],2,'0',STR_PAD_LEFT)) ? $age-=1 : $age;
    }
}
?>
